﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Commctrol_RecentlyBrowseHouse : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Cookies["houseIDInfo"] != null)
            {
                DataTable dtHouseLooked=getHouseLooked();
                if (dtHouseLooked == null)
                {
                    ltInfo.Text = @"<td colspan=""4"" style=""height:40px; color:#F67535;"" align=""center"">你还没有找过房哦，赶快行动吧！</td>";
                    return;
                }
                if (dtHouseLooked.Rows.Count == 0)
                {
                    ltInfo.Text = @"<td colspan=""4"" style=""height:40px;color:#F67535;"" align=""center"">你还没有找过房哦，赶快行动吧！</td>";
                    return;
                }
                rtLookedHouse.DataSource = dtHouseLooked;
                rtLookedHouse.DataBind();
            }
            else
            {
                ltInfo.Text = @"<td colspan=""4"" style=""height:40px;color:#F67535;"" align=""center"">你还没有找过房哦，赶快行动吧！</td>";
            }
            getHouseIDInfo();

        }
    }

    private void getHouseIDInfo()
    {
        string strUserID;
        Random aa = new Random();
        if (Request.Cookies["houseIDInfo"] != null)
        {
            Response.Cookies["houseIDInfo"].Value = checkHouseId(Request.Cookies["houseIDInfo"].Value);
        }
        else
        {
            HttpCookie cookie = new HttpCookie("houseIDInfo");
            strUserID = strHouseID;
            cookie.Value = strUserID;
            Response.Cookies.Add(cookie);
        }
    }

    private DataTable CreateDataTable()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("houseID");
        dt.Columns.Add("houseName");
        dt.Columns.Add("houseUnit");
        dt.Columns.Add("houseprice");
        dt.Columns.Add("housesize");

        for (int i = 0; i < 7; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = i.ToString();
            dr[1] = "花好月圆";
            dr[2] = "一室一厅";
            dr[3] = "300万";
            dr[4] = "80平米";
            dt.Rows.Add(dr);
        }
        return dt;
    }

    private string checkHouseId(string p_strHouseId)
    {
        if (p_strHouseId.IndexOf(strHouseID) >= 0)
        {
            return p_strHouseId;
        }
        else
        {
            return p_strHouseId + "," + strHouseID;
        }
    }


    public DataTable getHouseLooked()
    {
        DataTable dt = CreateDataTable();

        if (!string.IsNullOrEmpty(Request.Cookies["houseIDInfo"].Value))
        {
            dt.DefaultView.RowFilter = "houseID in(" + Request.Cookies["houseIDInfo"].Value + ")";

            return dt.DefaultView.ToTable();
        }
        else
        {
            return null;
        }
    }

    public string strHouseID
    {
        get
        {
            if (this.Request.QueryString["HouseId"] != null)
                return this.Request.QueryString["HouseId"].ToString();
            return "";
        }
    }
}
